from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class CustomerPagination(PageNumberPagination):
    page_query_param = 'pageNum'
    page_size_query_param = 'pageSize'  # ?pageNum=xx&pageSize=??
    page_size = 10  # 默认分页大小
    max_page_size = 999  # 默认最大分页数据量

    def get_paginated_response(self, data):
        msg_dict = {
            'msg': '操作成功',
            'pageNum': self.page.number,  # 当前分页
            'pageSize': self.get_page_size(self.request),  # 当前分页大小
            'totalPages': self.page.paginator.num_pages,  # 总分页数量
            'total': self.page.paginator.count,  # 数据总数
            'results': data  # 分页数据
        }
        if not data:
            msg_dict['msg'] = '暂无数据'
            msg_dict['results'] = []
        return Response(msg_dict)
